#include "browserwidget.h"

BrowserWidget::BrowserWidget( QMap<QString, QVariant> m,
                              QWidget * parent ) : QWidget(parent)
{
this->setAttribute(Qt::WA_DeleteOnClose);
config_map = m;

setupUi(this);

tablesListWidget->addAction(actionMeta);
tablesListWidget->addAction(actionCopyName);
tablesListWidget->addAction(actionExport);

QObject::connect( tablesListWidget,
                  SIGNAL( itemClicked( QListWidgetItem * ) ),
                  this,
                  SLOT( show_table( QListWidgetItem * ) ) );
QObject::connect( actionMeta,
                  SIGNAL( triggered() ),
                  this,
                  SLOT( show_metadata() ) );
QObject::connect( metaToolButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( show_metadata() ) );
QObject::connect( exportToolButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( export_table() ) );
QObject::connect( actionExport,
                  SIGNAL( triggered() ),
                  this,
                  SLOT( export_table() ) );
QObject::connect( copyToolButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( copy_name() ) );
QObject::connect( actionCopyName,
                  SIGNAL( triggered() ),
                  this,
                  SLOT( copy_name() ) );
QObject::connect( queryToolButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( exec_query() ) );
QObject::connect( exportQueryToolButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( exec_query_xml() ) );

connect_db();
}

BrowserWidget::~BrowserWidget()
{
}

int BrowserWidget::set_connection( const QString & driver,
                                   const QString & dbName,
                                   const QString & host,
                                   const QString & user,
                                   const QString & passwd,
                                   int port )
{
    if ( true == driver.isEmpty() )
    {
    return 0;
    }
QSqlDatabase db = QSqlDatabase::addDatabase( driver, "external" );
db.setDatabaseName(dbName);
db.setHostName(host);
db.setPort(port);
    if ( !db.open(user, passwd) )
    {
    QMessageBox::warning( this,
                          tr("Error"),
                          db.lastError().text() );
    return 0;
    }
tablesListWidget->addItems( db.tables( QSql::AllTables ) );
return 1;
}

void BrowserWidget::connect_db()
{
int ok = 0;

QString driverName = config_map.value("driver").toString();
QString databaseName = config_map.value("db").toString();
QString hostName = config_map.value("host").toString();
QString userName = config_map.value("user").toString();
QString password = config_map.value("pass").toString();
int port = config_map.value("pass").toInt();

ok = set_connection( driverName,
                     databaseName,
                     hostName,
                     userName,
                     password,
                     port );
    if ( ok == 0 )
    {
    close();
    }
}

void BrowserWidget::show_table( QListWidgetItem * item )
{
QSqlDatabase db = QSqlDatabase::database("external", false);
    if ( true != db.isOpen() )
    {
    QMessageBox::warning(this,
                         tr("Error"),
                         tr("Database is closed.") );
    return;
    }

QString t;
    if ( NULL != item )
    {
    t = item->text();
    }
    else
    {
    return;
    }

    if ( true == t.isEmpty() )
    {
    return;
    }

QAbstractItemModel * old_model = qobject_cast<QAbstractItemModel *>( resultTableView->model() );

QString query_source = QLatin1String("select * from ");
query_source.append( QLatin1String("\"") );
query_source.append( item->text() );
query_source.append( QLatin1String("\"") );

QSqlQueryModel * model = new QSqlQueryModel(queryTableView);
QSqlQuery query = QSqlQuery(query_source, db);
model->setQuery(query);
    if ( model->lastError().type() != QSqlError::NoError )
    {
    QMessageBox::warning( this,
                          tr("Database message"),
                          model->lastError().text() );
    }
resultTableView->setModel(model);

    if ( NULL != old_model )
    {
    delete old_model;
    old_model = NULL;
    }
}

void BrowserWidget::show_metadata()
{
QList<QListWidgetItem *> selected_items = tablesListWidget->selectedItems();
    if ( true == selected_items.isEmpty() )
    {
    return;
    }
get_metadata( selected_items.at(0) );
}

void BrowserWidget::get_metadata( QListWidgetItem * item )
{
QSqlDatabase db = QSqlDatabase::database("external", false);
    if ( true != db.isOpen() )
    {
    QMessageBox::warning(this,
                         tr("Error"),
                         tr("Database is closed.") );
    return;
    }

QString t;
    if ( NULL != item )
    {
    t = item->text();
    }
    else
    {
    return;
    }

    if ( true == t.isEmpty() )
    {
    return;
    }

QAbstractItemModel * old_model = qobject_cast<QAbstractItemModel *>( resultTableView->model() );

QSqlRecord record = db.record(t);

QStandardItemModel * model = new QStandardItemModel(resultTableView);

model->insertRows(0, record.count());
model->insertColumns(0, 2);
model->setHeaderData( 0, Qt::Horizontal, tr("Column name") );
model->setHeaderData( 1, Qt::Horizontal, tr("Data type") );

    for ( int i = 0; i < record.count(); ++i)
    {
    QSqlField field = record.field(i);
    model->setData( model->index( i, 0 ),
                    field.name() );
    model->setData( model->index( i, 1 ),
                    QString( QVariant::typeToName( field.type() ) ) );
    }

resultTableView->setModel(model);
    if ( NULL != old_model )
    {
    delete old_model;
    old_model = NULL;
    }
}

void BrowserWidget::close_connection()
{
    {
    QAbstractItemModel * old_model = qobject_cast<QAbstractItemModel *>(resultTableView->model());
    resultTableView->setModel(new QStandardItemModel);
        if ( NULL != old_model )
        {
        delete old_model;
        old_model = NULL;
        }
    }

    {
    QSqlQueryModel * old_q_model = qobject_cast<QSqlQueryModel *>( queryTableView->model() );
    queryTableView->setModel(new QSqlQueryModel);
        if ( NULL != old_q_model )
        {
        delete old_q_model;
        old_q_model = NULL;
        }
    }

    {
    QSqlDatabase db = QSqlDatabase::database("external", false);
        if ( true == db.isOpen() )
        {
        db.close();
        }
    }

QSqlDatabase::removeDatabase("external");    
}

void BrowserWidget::copy_name()
{
QClipboard * clipboard = QApplication::clipboard();
QList<QListWidgetItem *> selected_items = tablesListWidget->selectedItems();
    if ( true == selected_items.isEmpty() )
    {
    return;
    }
QListWidgetItem * item = selected_items.at(0);
QString table_name = QLatin1String("\"");
table_name.append( item->text() );
table_name.append( QLatin1String("\"") );
clipboard->setText(table_name);
}

void BrowserWidget::closeEvent( QCloseEvent * e )
{
close_connection();
e->accept();
}

void BrowserWidget::exec_query()
{
QSqlDatabase db = QSqlDatabase::database("external", false);
    if ( !db.isOpen() )
    {
    QMessageBox::warning( this,
                          tr("Error"),
                          tr("Database is closed.") );
    }

QString query_source = queryTextEdit->toPlainText();
    if ( query_source.isEmpty() )
    {
    return;
    }

QSqlQueryModel * old_model = qobject_cast<QSqlQueryModel *>( queryTableView->model() );

qApp->processEvents();

QRegExp rx0( QLatin1String("\\bDROP\\b"),     Qt::CaseInsensitive );
QRegExp rx1( QLatin1String("\\bUPDATE\\b"),    Qt::CaseInsensitive );
QRegExp rx2( QLatin1String("\\bCREATE\\b"),   Qt::CaseInsensitive );
QRegExp rx3( QLatin1String("\\bDELETE\\b"),   Qt::CaseInsensitive );
QRegExp rx4( QLatin1String("\\bTRUNCATE\\b"), Qt::CaseInsensitive );
QRegExp rx5( QLatin1String("\\bSET\\b"),      Qt::CaseInsensitive );
QRegExp rx6( QLatin1String("\\bINSERT\\b"),   Qt::CaseInsensitive );
    if ( query_source.contains(rx0) ||
         query_source.contains(rx1) ||
         query_source.contains(rx2) ||
         query_source.contains(rx3) ||
         query_source.contains(rx4) ||
         query_source.contains(rx5) ||
         query_source.contains(rx6) )
    {
    QMessageBox::critical( this,
                           tr("Error"),
                           tr("Query contains CREATE, DROP, UPDATE, SET, DELETE, INSERT or TRUNCATE keyword.") );
    return;
    }

QSqlQueryModel * model = new QSqlQueryModel(queryTableView);

QSqlQuery q = QSqlQuery(query_source, db);

    if ( true != q.isSelect() )
    {
    QMessageBox::critical( this,
                           tr("Error"),
                           tr("Query is not a valid SELECT query.\nCheck SQL syntax, error in colums and tables names.") );
    return;
    }

model->setQuery(q);
    if ( model->lastError().type() != QSqlError::NoError )
    {
    QMessageBox::warning( this,
                          tr("Database message"),
                          model->lastError().text() );
    }

qApp->processEvents();

queryTableView->setModel(model);

    if ( NULL != old_model )
    {
    delete old_model;
    old_model = NULL;
    }

queryTabWidget->setCurrentIndex(1);

qApp->processEvents();
}

void BrowserWidget::exec_query_xml()
{
QSqlDatabase db = QSqlDatabase::database("external", false);
    if ( !db.isOpen() )
    {
    QMessageBox::warning( this,
                          tr("Error"),
                          tr("Database is closed.") );
    }

QString query_source = queryTextEdit->toPlainText();
    if ( query_source.isEmpty() )
    {
    return;
    }

QSqlQueryModel * old_model = qobject_cast<QSqlQueryModel *>( queryTableView->model() );

qApp->processEvents();

QRegExp rx0( QLatin1String("\\bDROP\\b"),     Qt::CaseInsensitive );
QRegExp rx1( QLatin1String("\\bUPDATE\\b"),    Qt::CaseInsensitive );
QRegExp rx2( QLatin1String("\\bCREATE\\b"),   Qt::CaseInsensitive );
QRegExp rx3( QLatin1String("\\bDELETE\\b"),   Qt::CaseInsensitive );
QRegExp rx4( QLatin1String("\\bTRUNCATE\\b"), Qt::CaseInsensitive );
QRegExp rx5( QLatin1String("\\bSET\\b"),      Qt::CaseInsensitive );
QRegExp rx6( QLatin1String("\\bINSERT\\b"),   Qt::CaseInsensitive );
    if ( query_source.contains(rx0) ||
         query_source.contains(rx1) ||
         query_source.contains(rx2) ||
         query_source.contains(rx3) ||
         query_source.contains(rx4) ||
         query_source.contains(rx5) ||
         query_source.contains(rx6) )
    {
    QMessageBox::critical( this,
                           tr("Error"),
                           tr("Query contains CREATE, DROP, UPDATE, SET, DELETE, INSERT or TRUNCATE keyword.") );
    return;
    }

QSqlQueryModel * model = new QSqlQueryModel(queryTableView);
queryTableView->setModel(model);
qApp->processEvents();

    if ( NULL != old_model )
    {
    delete old_model;
    old_model = NULL;
    }

QSqlQuery q = QSqlQuery(query_source, db);

    if ( true != q.isSelect() )
    {
    QMessageBox::critical( this,
                           tr("Error"),
                           tr("Query is not a valid SELECT query.\nCheck SQL syntax, error in colums and tables names.") );
    return;
    }

QSqlRecord record = q.record();
int number_of_columns = record.count();

QMap<int, QStringList> m;
    for ( int i = 0; i < number_of_columns; ++i )
    {
    QSqlField field = record.field(i);
    QStringList l;
    l << QString( field.name() );
    l << QString( QVariant::typeToName( field.type() ) );
    m[i] = l;
    }

qApp->processEvents();

QString header_data = return_xml_header();
header_data.append( QLatin1String("<Table>\n") );
header_data.append( QLatin1String("<Column ss:Index=\"1\" ss:Width=\"80\"/>\n") );
header_data.append( QLatin1String("<Column ss:Index=\"") );
header_data.append( QString::number(number_of_columns) );
header_data.append( QLatin1String("\" ss:Width=\"120\"/>\n") );

header_data.append( QLatin1String("<Row>\n") );
    for ( int i = 0; i < number_of_columns; ++i )
    {
    QStringList l_tmp = m.value(i);
    header_data.append( QLatin1String("<Cell ss:StyleID=\"s21\"><Data ss:Type=\"String\">") );
    header_data.append( l_tmp.at(0) );
    header_data.append( QLatin1String("</Data></Cell>\n") );
    }
header_data.append( QLatin1String("</Row>\n") );
header_data.append( QLatin1String("\n") );

QString path = QFileDialog::getSaveFileName( this,
                                             tr("Select File"),
                                             QDir::homePath(),
                                             tr("MS XML Spreadsheet File (*.xml);;") );

    if ( true == path.isEmpty() )
    {
    return;
    }

QFile file_(path);
    if ( file_.exists() )
    {
    QMessageBox mBox;
    mBox.setWindowTitle( tr("File exists") );
    mBox.setIcon( QMessageBox::Question );
    mBox.setText( tr("Overwrite?") );
    mBox.setStandardButtons( QMessageBox::Yes | QMessageBox::No );
        switch ( mBox.exec() )
        {
            case QMessageBox::Yes:
            break;

            default:
            return;
            break;
        }
    file_.open(QIODevice::WriteOnly);
    file_.remove();
    }
QFile file(path);
bool opened = file.open(QIODevice::ReadWrite | QIODevice::Append );
    if ( true != opened )
    {
    QMessageBox::critical( this,
                           tr("Error"),
                           tr("Could not open file.") );
    return;
    }

const QByteArray header_ba = header_data.toUtf8();
const QByteArray & header_ba_ref = header_ba;
qint64 bytes = Q_INT64_C(-1);
bytes = file.write(header_ba_ref);

    while ( q.next() )
    {
    QString row_data = QLatin1String("<Row>");
        
        for ( int y = 0; y < number_of_columns; ++y )
        {
        QVariant v = q.value(y);
            if ( true != v.isNull() &&
                 true == v.isValid() )
            {
            row_data.append( QLatin1String("<Cell ss:StyleID=\"Default\" ss:Index=\"") );
            row_data.append( QString::number( (y + 1) ) );
            row_data.append( QLatin1String("\">") );

            QStringList t = m.value(y);
                if ( t.size() == 2 )
                {
                    if ( t.at(1).toUpper() == QString( QLatin1String("INT") ) )
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"Number\">") );
                    row_data.append( QString::number( v.toInt() ) );
                    row_data.append( QLatin1String("</Data>") );
                    }
                    else if ( t.at(1).toUpper() == QString( QLatin1String("UINT") ) )
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"Number\">") );
                    row_data.append( QString::number( v.toUInt() ) );
                    row_data.append( QLatin1String("</Data>") );
                    }
                    else if ( t.at(1).toUpper() == QString( QLatin1String("DOUBLE") ) )
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"Number\">") );
                    row_data.append( QString::number( v.toDouble() ) );
                    row_data.append( QLatin1String("</Data>") );
                    }
                    else if ( t.at(1).toUpper() == QString( QLatin1String("QLONGLONG") ) )
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"Number\">") );
                    row_data.append( QString::number( v.toLongLong() ) );
                    row_data.append( QLatin1String("</Data>") );
                    }
                    else if ( t.at(1).toUpper() == QString( QLatin1String("QULONGLONG") ) )
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"Number\">") );
                    row_data.append( QString::number( v.toULongLong() ) );
                    row_data.append( QLatin1String("</Data>") );
                    }
                    else if ( t.at(1).toUpper() == QString( QLatin1String("QBYTEARRAY") ) )
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"String\">") );
                    row_data.append( QLatin1String("[Binary data skipped.]") );
                    row_data.append( QLatin1String("</Data>") );
                    }
                    else
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"String\">") );
                    row_data.append( v.toString() );
                    row_data.append( QLatin1String("</Data>") );
                    }
                }
                else
                {
                QString message = tr("Internal error.");
                message.append( QLatin1String("t.size() != 2") ); 
                QMessageBox::critical( this,
                                       tr("Error"),
                                       message );
                file.close();
                file.remove();
                return;
                }
            row_data.append( QLatin1String("</Cell>\n") );
            }
        }
    row_data.append( QLatin1String("</Row>") );
    const QByteArray row_ba = row_data.toUtf8();
    const QByteArray & row_ba_ref = row_ba;
    bytes += file.write(row_ba_ref);
    }

QString end_data = QLatin1String("</Table>");
end_data.append( return_xml_end() );

const QByteArray end_ba = end_data.toUtf8();
const QByteArray & end_ba_ref = end_ba;
bytes += file.write(end_ba_ref);

bool success = false;
success = file.flush();
qApp->processEvents();
file.close();
q.clear();
qApp->processEvents();

QMessageBox::information( this,
                          tr("Information"),
                          tr("Export successful.") );

}

void BrowserWidget::export_table()
{
QList<QListWidgetItem *> selected_items = tablesListWidget->selectedItems();
    if ( true == selected_items.isEmpty() )
    {
    return;
    }

QSqlDatabase db = QSqlDatabase::database("external", false);
    if ( !db.isOpen() )
    {
    QMessageBox::warning( this,
                          tr("Error"),
                          tr("Database is closed.") );
    }

QString t = (selected_items.at(0))->text();
QString query_source = "SELECT * FROM ";
query_source.append( QLatin1String("\"") );
query_source.append(t);
query_source.append( QLatin1String("\"") );

    {
    QAbstractItemModel * old_model = qobject_cast<QAbstractItemModel *>(resultTableView->model());
    resultTableView->setModel(new QStandardItemModel);
        if ( NULL != old_model )
        {
        delete old_model;
        old_model = NULL;
        }
    }

    {
    QSqlQueryModel * old_q_model = qobject_cast<QSqlQueryModel *>( queryTableView->model() );
    queryTableView->setModel(new QSqlQueryModel);
        if ( NULL != old_q_model )
        {
        delete old_q_model;
        old_q_model = NULL;
        }
    }

QSqlQuery q = QSqlQuery(query_source, db);

    if ( q.lastError().type() != QSqlError::NoError )
    {
    QMessageBox::critical( this,
                           tr("Database message"),
                           q.lastError().text() );
    return;
    }

QSqlRecord record = q.record();
int number_of_columns = record.count();

QMap<int, QStringList> m;
    for ( int i = 0; i < number_of_columns; ++i )
    {
    QSqlField field = record.field(i);
    QStringList l;
    l << QString( field.name() );
    l << QString( QVariant::typeToName( field.type() ) );
    m[i] = l;
    }

qApp->processEvents();

QString header_data = return_xml_header();
header_data.append( QLatin1String("<Table>\n") );
header_data.append( QLatin1String("<Column ss:Index=\"1\" ss:Width=\"80\"/>\n") );
header_data.append( QLatin1String("<Column ss:Index=\"") );
header_data.append( QString::number(number_of_columns) );
header_data.append( QLatin1String("\" ss:Width=\"120\"/>\n") );

header_data.append( QLatin1String("<Row>\n") );
    for ( int i = 0; i < number_of_columns; ++i )
    {
    QStringList l_tmp = m.value(i);
    header_data.append( QLatin1String("<Cell ss:StyleID=\"s21\"><Data ss:Type=\"String\">") );
    header_data.append( l_tmp.at(0) );
    header_data.append( QLatin1String("</Data></Cell>\n") );
    }
header_data.append( QLatin1String("</Row>\n") );
header_data.append( QLatin1String("\n") );

QString path = QFileDialog::getSaveFileName( this,
                                             tr("Select File"),
                                             QDir::homePath(),
                                             tr("MS XML Spreadsheet File (*.xml);;") );

    if ( true == path.isEmpty() )
    {
    return;
    }

QFile file_(path);
    if ( file_.exists() )
    {
    QMessageBox mBox;
    mBox.setWindowTitle( tr("File exists") );
    mBox.setIcon( QMessageBox::Question );
    mBox.setText( tr("Overwrite?") );
    mBox.setStandardButtons( QMessageBox::Yes | QMessageBox::No );
        switch ( mBox.exec() )
        {
            case QMessageBox::Yes:
            break;

            default:
            return;
            break;
        }
    file_.open(QIODevice::WriteOnly);
    file_.remove();
    }
QFile file(path);
bool opened = file.open(QIODevice::ReadWrite | QIODevice::Append );
    if ( true != opened )
    {
    QMessageBox::critical( this,
                           tr("Error"),
                           tr("Could not open file.") );
    return;
    }

const QByteArray header_ba = header_data.toUtf8();
const QByteArray & header_ba_ref = header_ba;
qint64 bytes = Q_INT64_C(-1);
bytes = file.write(header_ba_ref);

    while ( q.next() )
    {
    QString row_data = QLatin1String("<Row>");
        
        for ( int y = 0; y < number_of_columns; ++y )
        {
        QVariant v = q.value(y);
            if ( true != v.isNull() &&
                 true == v.isValid() )
            {
            row_data.append( QLatin1String("<Cell ss:StyleID=\"Default\" ss:Index=\"") );
            row_data.append( QString::number( (y + 1) ) );
            row_data.append( QLatin1String("\">") );

            QStringList t = m.value(y);
                if ( t.size() == 2 )
                {
                    if ( t.at(1).toUpper() == QString( QLatin1String("INT") ) )
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"Number\">") );
                    row_data.append( QString::number( v.toInt() ) );
                    row_data.append( QLatin1String("</Data>") );
                    }
                    else if ( t.at(1).toUpper() == QString( QLatin1String("UINT") ) )
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"Number\">") );
                    row_data.append( QString::number( v.toUInt() ) );
                    row_data.append( QLatin1String("</Data>") );
                    }
                    else if ( t.at(1).toUpper() == QString( QLatin1String("DOUBLE") ) )
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"Number\">") );
                    row_data.append( QString::number( v.toDouble() ) );
                    row_data.append( QLatin1String("</Data>") );
                    }
                    else if ( t.at(1).toUpper() == QString( QLatin1String("QLONGLONG") ) )
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"Number\">") );
                    row_data.append( QString::number( v.toLongLong() ) );
                    row_data.append( QLatin1String("</Data>") );
                    }
                    else if ( t.at(1).toUpper() == QString( QLatin1String("QULONGLONG") ) )
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"Number\">") );
                    row_data.append( QString::number( v.toULongLong() ) );
                    row_data.append( QLatin1String("</Data>") );
                    }
                    else if ( t.at(1).toUpper() == QString( QLatin1String("QBYTEARRAY") ) )
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"String\">") );
                    row_data.append( QLatin1String("[Binary data skipped.]") );
                    row_data.append( QLatin1String("</Data>") );
                    }
                    else
                    {
                    row_data.append( QLatin1String("<Data ss:Type=\"String\">") );
                    row_data.append( v.toString() );
                    row_data.append( QLatin1String("</Data>") );
                    }
                }
                else
                {
                QString message = tr("Internal error.");
                message.append( QLatin1String("t.size() != 2") ); 
                QMessageBox::critical( this,
                                       tr("Error"),
                                       message );
                file.close();
                file.remove();
                return;
                }
            row_data.append( QLatin1String("</Cell>\n") );
            }
        }
    row_data.append( QLatin1String("</Row>") );
    const QByteArray row_ba = row_data.toUtf8();
    const QByteArray & row_ba_ref = row_ba;
    bytes += file.write(row_ba_ref);
    }

QString end_data = QLatin1String("</Table>");
end_data.append( return_xml_end() );

const QByteArray end_ba = end_data.toUtf8();
const QByteArray & end_ba_ref = end_ba;
bytes += file.write(end_ba_ref);

bool success = false;
success = file.flush();
qApp->processEvents();
file.close();
q.clear();
qApp->processEvents();

QMessageBox::information( this,
                          tr("Information"),
                          tr("Export successful.") );

}

QString BrowserWidget::return_xml_header()
{
QString header_data = QLatin1String("<?xml version=\"1.0\"?>\n");
header_data.append( QLatin1String("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n") );
header_data.append( QLatin1String("xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n") );
header_data.append( QLatin1String("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n") );
header_data.append( QLatin1String("xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\n") );
header_data.append( QLatin1String("xmlns:html=\"http://www.w3.org/TR/REC-html40\"\n") );
header_data.append( QLatin1String("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n") );
header_data.append( QLatin1String("xmlns:x2=\"http://schemas.microsoft.com/office/excel/2003/xml\">\n") );
header_data.append( QLatin1String("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">\n") );
header_data.append( QLatin1String("</DocumentProperties>\n") );
header_data.append( QLatin1String("<OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\">\n") );
header_data.append( QLatin1String("</OfficeDocumentSettings>\n") );
header_data.append( QLatin1String("<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n") );
header_data.append( QLatin1String("<ProtectStructure>False</ProtectStructure>\n") );
header_data.append( QLatin1String("<ProtectWindows>False</ProtectWindows>\n") );
header_data.append( QLatin1String("</ExcelWorkbook>\n") );
header_data.append( QLatin1String("<Styles>\n") );
header_data.append( QLatin1String("<Style ss:ID=\"Default\" ss:Name=\"Normal\">\n") );
header_data.append( QLatin1String("<Alignment ss:Vertical=\"Bottom\"/>\n") );
header_data.append( QLatin1String("<Borders/>\n") );
header_data.append( QLatin1String("<Font/>\n") );
header_data.append( QLatin1String("<Interior/>\n") );
header_data.append( QLatin1String("<NumberFormat/>\n") );
header_data.append( QLatin1String("<Protection/>\n") );
header_data.append( QLatin1String("</Style>\n") );
header_data.append( QLatin1String("<Style ss:ID=\"s21\">\n") );
header_data.append( QLatin1String("<Font ss:Color=\"#FFFFFF\"/>\n") );
header_data.append( QLatin1String("<Interior ss:Color=\"#333399\" ss:Pattern=\"Solid\"/>\n") );
header_data.append( QLatin1String("</Style>\n") );
header_data.append( QLatin1String("</Styles>\n") );
header_data.append( QLatin1String("<Worksheet ss:Name=\"") );
header_data.append( QLatin1String("query") );
header_data.append( QLatin1String("\">\n") );
return header_data;
}

QString BrowserWidget::return_xml_end()
{
QString end_data = QLatin1String("");
end_data.append( QLatin1String("<WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">") );
end_data.append( QLatin1String("<Selected/>\n") );
end_data.append( QLatin1String("<ProtectObjects>False</ProtectObjects>\n") );
end_data.append( QLatin1String("<ProtectScenarios>False</ProtectScenarios>\n") );
end_data.append( QLatin1String("</WorksheetOptions>\n") );
end_data.append( QLatin1String("<x:WorksheetOptions/>\n") );
end_data.append( QLatin1String("</Worksheet>\n") );
end_data.append( QLatin1String("</Workbook>\n") );
return end_data;
}
